/*
 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
 * All Rights Reserved.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it would be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write the Free Software Foundation,
 * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */
#ifndef __DFS_BMAP_UTIL_H__
#define	__DFS_BMAP_UTIL_H__

/* Kernel only BMAP related definitions and functions */

struct dfs_bmbt_irec;
struct dfs_bmap_free_item;
struct dfs_ifork;
struct dfs_inode;
struct dfs_mount;
struct dfs_trans;

/*
 * Argument structure for dfs_bmap_alloc.
 */
struct dfs_bmalloca {
	dfs_fsblock_t		*firstblock; /* i/o first block allocated */
	struct dfs_bmap_free	*flist;	/* bmap freelist */
	struct dfs_trans	*tp;	/* transaction pointer */
	struct dfs_inode	*ip;	/* incore inode pointer */
	struct dfs_bmbt_irec	prev;	/* extent before the new one */
	struct dfs_bmbt_irec	got;	/* extent after, or delayed */

	dfs_fileoff_t		offset;	/* offset in file filling in */
	dfs_extlen_t		length;	/* i/o length asked/allocated */
	dfs_fsblock_t		blkno;	/* starting block of new extent */

	struct dfs_btree_cur	*cur;	/* btree cursor */
	dfs_extnum_t		idx;	/* current extent index */
	int			nallocs;/* number of extents alloc'd */
	int			logflags;/* flags for transaction logging */

	dfs_extlen_t		total;	/* total blocks needed for xaction */
	dfs_extlen_t		minlen;	/* minimum allocation size (blocks) */
	dfs_extlen_t		minleft; /* amount must be left after alloc */
	char			eof;	/* set if allocating past last extent */
	char			wasdel;	/* replacing a delayed allocation */
	char			userdata;/* set if is user data */
	char			aeof;	/* allocated space at eof */
	char			conv;	/* overwriting unwritten extents */
	char			stack_switch;
	int			flags;
	struct completion	*done;
	struct work_struct	work;
	int			result;
};

int	dfs_bmap_finish(struct dfs_trans **tp, struct dfs_bmap_free *flist,
			int *committed);
int	dfs_bmap_rtalloc(struct dfs_bmalloca *ap);
int	dfs_bmapi_allocate(struct dfs_bmalloca *args);
int	__dfs_bmapi_allocate(struct dfs_bmalloca *args);
int	dfs_bmap_eof(struct dfs_inode *ip, dfs_fileoff_t endoff,
		     int whichfork, int *eof);
int	dfs_bmap_count_blocks(struct dfs_trans *tp, struct dfs_inode *ip,
			      int whichfork, int *count);
int	dfs_bmap_punch_delalloc_range(struct dfs_inode *ip,
		dfs_fileoff_t start_fsb, dfs_fileoff_t length);

/* bmap to userspace formatter - copy to user & advance pointer */
typedef int (*dfs_bmap_format_t)(void **, struct getbmapx *, int *);
int	dfs_getbmap(struct dfs_inode *ip, struct getbmapx *bmv,
		dfs_bmap_format_t formatter, void *arg);

/* functions in dfs_bmap.c that are only needed by dfs_bmap_util.c */
void	dfs_bmap_del_free(struct dfs_bmap_free *flist,
			  struct dfs_bmap_free_item *prev,
			  struct dfs_bmap_free_item *free);
int	dfs_bmap_extsize_align(struct dfs_mount *mp, struct dfs_bmbt_irec *gotp,
			       struct dfs_bmbt_irec *prevp, dfs_extlen_t extsz,
			       int rt, int eof, int delay, int convert,
			       dfs_fileoff_t *offp, dfs_extlen_t *lenp);
void	dfs_bmap_adjacent(struct dfs_bmalloca *ap);
int	dfs_bmap_last_extent(struct dfs_trans *tp, struct dfs_inode *ip,
			     int whichfork, struct dfs_bmbt_irec *rec,
			     int *is_empty);

/* preallocation and hole punch interface */
int	dfs_alloc_file_space(struct dfs_inode *ip, dfs_off_t offset,
			     dfs_off_t len, int alloc_type);
int	dfs_free_file_space(struct dfs_inode *ip, dfs_off_t offset,
			    dfs_off_t len);
int	dfs_zero_file_space(struct dfs_inode *ip, dfs_off_t offset,
			    dfs_off_t len);

/* EOF block manipulation functions */
bool	dfs_can_free_eofblocks(struct dfs_inode *ip, bool force);
int	dfs_free_eofblocks(struct dfs_mount *mp, struct dfs_inode *ip,
			   bool need_iolock);

int	dfs_swap_extents(struct dfs_inode *ip, struct dfs_inode *tip,
			 struct dfs_swapext *sx);

dfs_daddr_t dfs_fsb_to_db(struct dfs_inode *ip, dfs_fsblock_t fsb);

#endif	/* __DFS_BMAP_UTIL_H__ */
